package com.android.personal.kotlin.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import com.android.personal.kotlin.R
import com.android.personal.kotlin.control.WatchedImp
import com.android.personal.kotlin.control.IssueKey
import com.android.personal.kotlin.control.Watcher
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.toolbar_layout.*

class MainActivity : BaseAppCompatActivity(), View.OnClickListener, Watcher {
    override fun onDataChanged(issueKey: IssueKey?, o: Any?) {
        login.text = "再登陆一次 IssueKey:" + issueKey

    }

    override fun onClick(v: View?) {
        when(v!!.id) {
            R.id.toolbar_title -> Toast.makeText(this, "不要点了,头疼", Toast.LENGTH_SHORT).show()
            R.id.toolbar_subtitle -> Toast.makeText(this, "我只是一个副标题", Toast.LENGTH_SHORT).show()
        }
    }

    companion object {
        val NAME = "name"
        val PASSWORD = "password"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initView()
    }

    fun initView() {
        WatchedImp.getInstance().addWatcher(this)
        getToolbarTitle().text = "主界面"
        getSubTitle().text = "没什么了"

        login.setOnClickListener {
            if (!TextUtils.isEmpty(name.text) && !TextUtils.isEmpty(password.text)) {
                startActivity(Intent(this, LoginResultActivity::class.java)
                        .putExtra(NAME, name.text)
                        .putExtra(PASSWORD, password.text))
            } else {
                Toast.makeText(this, "请输入信息", Toast.LENGTH_SHORT).show()
            }
        }

        toolbar_subtitle.setOnClickListener(this)
        toolbar_title.setOnClickListener(this)
        goListView.setOnClickListener { startActivity(Intent(this, ListViewActivity::class.java)) }
        goGridView.setOnClickListener { startActivity(Intent(this, GridViewAtivity::class.java)) }
        goRecyclerView.setOnClickListener { startActivity(Intent(this, RecyclerViewActivity::class.java)) }
        goSwipeListView.setOnClickListener { startActivity(Intent(this, SwipeListViewActivity::class.java)) }
    }

    override fun isShowBacking(): Boolean {
        return false
    }
}
